package com.ikas.ai.server.module.flow.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.flow.dao.FlowNodeExecDAO;
import com.ikas.ai.server.module.flow.dto.FlowInstanceAndExecDTO;
import com.ikas.ai.server.module.flow.model.FlowInstance;
import com.ikas.ai.server.module.flow.model.FlowNodeExec;
import com.ikas.ai.server.module.flow.model.FlowNodeHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 流程状态执行服务类
 * @Author: qi.yilai
 * @Date: 2023/1/31 11:51
 * @Version: 1.0.0
 **/
@Service
public class FlowNodeExecService extends ServiceImpl<FlowNodeExecDAO, FlowNodeExec> {

    @Lazy
    @Autowired
    private FlowInstanceService flowInstanceService;

    @Autowired
    private FlowNodeHistoryService flowNodeHistoryService;

    /**
     * 获取流程和节点执行状态
     *
     * @param flowInstanceId
     * @return
     */
    public FlowInstanceAndExecDTO getFlowInstanceAndExec(Long flowInstanceId) {
        FlowInstance flowInstance = flowInstanceService.getById(flowInstanceId);
        List<FlowNodeExec> list = getFlowNodeExecs(flowInstanceId);
        List<FlowNodeHistory> nodes = flowNodeHistoryService.getFlowNodeHistories(flowInstanceId);
        return FlowInstanceAndExecDTO.builder().flowInstance(flowInstance).execs(list).nodes(nodes).build();
    }

    /**
     * 获取流程节点执行结果
     *
     * @param flowInstanceId
     * @return
     */
    public List<FlowNodeExec> getFlowNodeExecs(Long flowInstanceId) {
        LambdaQueryWrapper<FlowNodeExec> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FlowNodeExec::getFlowInstanceId, flowInstanceId);
        return list(lambdaQueryWrapper);
    }
}
